﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using C1.Silverlight.DataGrid.Summaries;

namespace DYSales.Views.Stock
{
    public partial class mgUnstock_page : Page
    {
        public mgUnstock_page()
        {
            InitializeComponent();
            dg_usts.AutoGeneratingColumn += new EventHandler<C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs>(dg_usts_AutoGeneratingColumn);
            Loaded += new RoutedEventHandler(mgUnstock_page_Loaded);
            bt_删除.Click += new RoutedEventHandler(bt_删除_Click);
        }

        void dg_usts_AutoGeneratingColumn(object sender, C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.PropertyType.Equals(typeof(decimal)) || e.Property.PropertyType.Equals(typeof(double)))
            {
                e.Column.Format = "0.##";
            }
            if (e.Property.PropertyType.Equals(typeof(DateTime)))
            {
                e.Column.Format = "yyyy/MM/dd HH:mm:ss";
            }
            #region 减少列
            string headername = e.Column.Header.ToString();
            switch (headername)
            {
                case "ID":
                case "外键":
                    e.Cancel = true;
                    break;

                case "退货数量":
                    DataGridAggregate.SetAggregateFunctions(e.Column, new DataGridAggregatesCollection { new DataGridAggregateSum() });
                    break;
            }
            #endregion
        }

        void bt_删除_Click(object sender, RoutedEventArgs e)
        {
            if (dg_usts.Selection.SelectedRows.Count >= 1)
            {
                var box = other.GetMessageBox();
                box.Title = "东雅进销存提示";
                box.tbk_text.Text = "确定要删除选中的记录吗?";
                box.Show();
                box.Closed += new EventHandler(box_Closed);
            }
        }

        void box_Closed(object sender, EventArgs e)
        {
            var cwin = sender as cwin_Question;
            cwin.Closed -= new EventHandler(box_Closed);
            if (cwin.DialogResult.Value)
            {
                dm.getDS().Load(dm.getDS().GetPdTablesQuery(), lp =>
                {
                    foreach (var item in dg_usts.Selection.SelectedRows)
                    {
                        var sa = item.DataItem as Web.StockDel;
                        if (sa != null)
                        {
                            var pd = lp.Entities.FirstOrDefault(p => p.p_商品名称.Equals(sa.st_商品名称));
                            if (pd != null)
                            {
                                pd.p_库存数量 += sa.st_退货数量;
                            }
                            dm.getDS().StockDels.Remove(sa);
                        }
                    }
                    dm.getDS().SubmitChanges(lo => { dg_usts.DataContext = dm.getDS().StockDels; }, null);
                }, null);
            }
        }

        void mgUnstock_page_Loaded(object sender, RoutedEventArgs e)
        {
            refresh();
        }

        void refresh()
        {
            dm.getDS().Load(dm.getDS().GetStockDelsQuery(), lo =>
            {
                dg_usts.DataContext = lo.Entities;
            }, null);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
